package threadLearn.v2;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 使用codition
 * @Description:
 * @author wanglong
 * @createDate: 2019年3月25日 下午2:27:57
 */
public class ConditionLearn implements Runnable{

	private ReentrantLock lock;
	
	private Condition condition;
	public ConditionLearn(ReentrantLock lock,Condition condition) {
		this.lock = lock;
		this.condition = condition;
	}
	
	@Override
	public void run() {
		
		try {
			lock.lock();
			System.out.println("开始："+Thread.currentThread());
			/**
			 * 再次学习wait 释放锁，sleep不释放锁
			 */
			condition.await(5, TimeUnit.SECONDS);
			//Thread.sleep(4000);
			System.out.println(Thread.currentThread()+": 结束");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public static void main(String[] args) {
		ReentrantLock lock = new ReentrantLock();
		Condition condition = lock.newCondition();
		for (int i = 0; i < 3; i++) {
			new Thread(new ConditionLearn(lock,condition)).start();
		}
	}

}
